package com.Gui;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import com.logic.Gamelogic;

import javax.swing.*;

public class MyCLicKListener implements MouseListener {
    Gamelogic logic = new Gamelogic();
    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y =e.getY();
        x = x - 100;
        y = y - 70;
        int c = x/50;
        int r = y/50;
        if (x % 50 >= 25){
            c++;
        }

        if (y % 50 >= 25){
            r++;
        }
        System.out.println(c+"行"+r+"列");


        //落子点是否合法
        boolean flag = logic.playChest(r,c);

        if (flag) {
            Graphics g = e.getComponent().getGraphics();
            //计算棋子的坐标点
            int posittionX = c * 50 + 100 - 25;
            int posittionY = r * 50 + 70 - 25;
            //根据role开始画棋子
            if (logic.role == 1) {
                g.setColor(Color.black);
                g.fillOval(posittionX, posittionY, 50, 50);
            } else {
                g.setColor(Color.white);
                g.fillOval(posittionX, posittionY, 50, 50);
            }
            //是否胜利
            if (logic.iswinner(r,c)){
                //成功之后，先移除鼠标监听
                e.getComponent().removeMouseListener(this);
                //使用小弹框，提示成功
                JDialog dialog = new JDialog();
                dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                dialog.setSize(300,100);
                dialog.setLocation(430,470);
                dialog.setTitle("恭喜你");
                JPanel panel = new JPanel();
                panel.setSize(300,470);
                panel.setLocation(0,0);
                if (logic.role == 1){
                    panel.add(new JLabel("黑棋获胜"));
                }
                else {
                    panel.add(new JLabel("白棋获胜"));
                }
                dialog.setContentPane(panel);
                dialog.setVisible(true);
            }
            logic.role = 3 - logic.role;
        }
    }
    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
